package com.cv.framework.common.validation;

import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * 密码校验器
 *
 * @author Charles_XDXD
 */
public class PasswordValidator implements ConstraintValidator<Password, String> {

    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,16}$");

    @Override
    public boolean isValid(String password, ConstraintValidatorContext context) {
        if (StrUtil.isEmpty(password)) {
            return false;
        }

        return PASSWORD_PATTERN.matcher(password).matches();
    }

}